package com.hunter.download.queue;

import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;

/**
 * @author SnowDragon
 * @date 2021/12/7
 * @since v1.0.0
 * Description:
 */
public class SameThreadExchanger<V> extends Exchanger<V> {

    private V v;

    public SameThreadExchanger() {
    }

    void setV(V v) {
        this.v = v;
    }

    @Override
    public V exchange(V x, long timeout, TimeUnit unit) {
        return exchange(v);
    }

    @Override
    public V exchange(V x) {
        try {
            V v = this.v;
            return v;
        } finally {
            this.v = null;
        }
    }
}